#include "getword.h"

#include <assert.h>

int getword(FILE *fp, char *buf, int size, int first(int c), int rest(int c))
{
    assert(fp && buf && size > 1 && first && rest);

    int i = 0;
    int c;
    while ((c = fgetc(fp)) != EOF) {
        if (first(c)) {
            if (i < size - 1)
                buf[i++] = c;
            break;
        }
    }

    while ((c = fgetc(fp)) != EOF && rest(c)) {
        if (i < size - 1)
            buf[i++] = c;
    }
    buf[i] = '\0';

    if (c != EOF)
        ungetc(c, fp);

    return i > 0;
}

